<template>
  <div class="check-task">
    <div>
            <!-- 报告类型 -->
      <eagle-radio-group
      v-if="ReportType=='Report'"
        v-model="data.ReportType"
        required
        name="radio"
        label="报告类型"
        :dataSource="ReportTypeData"
      ></eagle-radio-group>
      <!-- 报告模板 -->
      <eagle-radio-group
        v-model="data.ReportCode"
        required
        name="radio"
        label="报告模板"
        :dataSource="data.ReportDefines"
        :readonly="isRead"
      ></eagle-radio-group>
      <eagle-radio-group
        v-if="codesList.length > 1 && ReportType == 'Report'"
        required
        name="radio"
        label="生成设置"
        v-model="data.GenerateOption"
      >
        <eagle-radio
          name="2"
          style="margin: 0 10px 10px 0"
        >合并生成一个报告</eagle-radio>
        <eagle-radio
          name="1"
          style="margin: 0 10px 10px 0"
        >
          每个任务独立生成报告
        </eagle-radio>
      </eagle-radio-group>
      <eagle-input
        v-if="data.GenerateOption == 2"
        label="报告名称"
        required
        v-model="data.Name"
        rows="3"
        autosize
        type="textarea"
        placeholder="请输入报告名称"
        isInputText
      ></eagle-input>
      <eagle-radio-group
        v-model="data.OrderType"
        name="radio"
        label="隐患排序类型"
        :dataSource="data.OrderTypeList"
        direction="horizontal"
      ></eagle-radio-group>
      <!-- 日期 Date-->
      <eagle-date
        label="开始日期"
        :date.sync="data.StartDate"
        required
        placeholder="点击选择日期"
      ></eagle-date>
      <eagle-date
        label="结束日期"
        :date.sync="data.EndDate"
        required
        placeholder="点击选择日期"
      ></eagle-date>
      <eagle-input
        :multiRow="true"
        title="总结和建议"
        type="textarea"
        v-model="data.CheckRequirement"
        rows="3"
        autosize
        isInputText
        placeholder="请输入总结和建议"
      ></eagle-input>

      <div class="bottom-btns">
        <eagle-button
          class="submit-btn"
          type="primary"
          @click="save()"
        >
          生成
        </eagle-button>
      </div>
    </div>
  </div>
</template>

<script>
import { formatDate, getDate } from "@/utils/getDate";

export default {
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      url:'/DangerReport/GetGenerateEntity',
      url_generateReport:'/DangerReport/GenerateReport',
      title: "",
      codes: this.$route.query.codes,
      type: this.$route.query.type,
      isRead:true,
      ReportType:this.$route.query.ReportType,
      filter: {
        id: 0,
        type: this.$route.query.type,
        lgType: "normal",
        reportType: this.$route.query.reportType || 1,
      },
      data: {},
      uploader: [{ url: "https://img.yzcdn.cn/vant/leaf.jpg" }],
      showCalendar: false,
      idx: "",
      show: false,
      showChooseTask: false,
      activeIcon: require("@/assets/Images/AppIcon/item_check_transparent.png"),
      codesList: [],
      reportCodeList: [],

      ReportTypeData:[{
        ID:1,
        Name:'检查报告'
      },
      {
        ID:2,
        Name:'整改报告'
      }
    ]
    };
  },
  watch:{
    'data.ReportType':{
      handler(olde,new2){
        this.data.ReportCode=olde==1?'DangerCheckCoverReportJG':'DangerCorrectiveCoverReportJG'
      }
    }
  },
  computed:{
  
  },
  created() {
    if(this.$store.state.CompanyType=='JG'){
      this.filter.lgType = "supervision";
      this.url='/DangerReportJG/GetGenerateEntity'
      this.url_generateReport='/DangerReportJG/GenerateReport'
      
      if(this.$route.query.isRead=='false'){
        this.isRead=false
      }
    }
    if (this.$route.query.codes) {
      this.getGenerateEntity();
      let codesList = this.codes.split(",");
      
      this.codesList = codesList;
    }
  },
  activated() {},
  methods: {
    getGenerateEntity() {
      this.filter.codes = this.codes;
      let _this = this;
      _this.$axios
        .get(this.url, { params: _this.filter })
        .then((res) => {
          res.data.OrderTypeList.unshift({
            ID: "0",
            Name: "不限",
            IsDefault:false
          });
          res.data.OrderType = '0';
          res.data.ReportCode = res.data.ReportDefines[0].ID;
          res.data.StartDate = getDate(res.data.StartDate);
          res.data.EndDate = getDate(res.data.EndDate);
          _this.data = res.data;
          if(this.$route.query.Name){
            this.data.Name=this.$route.query.Name
          }
        });
    },
    // 选择检查类型
    checkTypeName() {
      this.show = true;
    },
    // 打开日历
    openCalendar(idx) {
      this.idx = idx;
      this.showCalendar = true;
    },
    onConfirm(date) {
      const dateTime = formatDate(date);
      this.data[this.idx] = getDate(dateTime);
      this.showCalendar = false;
    },
    save() {
      console.log(this.data, "this.data");
      if (!this.data.ReportCode) {
        return this.$toast("请选择报告模板");
      }
      if (this.data.GenerateOption == 2) {
        if (!this.data.Name) {
          return this.$toast("请填写报告名称");
        }
      }

      if (!this.data.StartDate) {
        return this.$toast("请选择开始日期");
      }
      if (!this.data.EndDate) {
        return this.$toast("请选择结束日期");
      }
      this.loading = true;
      let form = {
        Codes: this.data.Codes,
        ReportCode: this.data.ReportCode,
        ReportType: this.data.ReportType,
        GenerateOption: this.data.GenerateOption,
        // Type: this.data.Type,
        //Task 不行 换TaskDetail 又可以了
        // Type:'TaskDetail',
        Type:this.type,
        StartDate: this.data.StartDate,
        EndDate: this.data.EndDate,
        Remarks: this.data.CheckRequirement,
        Name: this.data.Name,
      };
      let _this = this;
      _this.$axios
        .post(this.url_generateReport, form)
        .then((res) => {
          _this.$toast("生成成功");
          _this.loading = false;
          setTimeout(() => {
            _this.title = "报告详情";
            _this.$router.push({
              name: "CheckReportDetail",
              query: {
                url:
                  // "https://afx.ehsway.com" +
                  process.env.VUE_APP_BASE_API+
                  // "/Plugins/pdfjs/web/viewer.html?file=" +
                  res.data.Attachs,
                signState:res.data.SignState,
                // IDS:this.$route.query.ids
                id:res.data.ID,
                isSign:false
              },
            });
          }, 300);
        })
        .catch((err) => {
          console.log("err", err);
        });
    },
  },
};
</script>

<style lang="less" scoped>
.check-task {
  background-color: #e8f1f8;
  padding-bottom: 20px;
}
.problem {
  margin-bottom: 10px;
}
.bottom-btns {
  margin-top: 10px;
  padding: 0 20px;

  .submit-btn {
    width: 100%;
    height: 30px;
    font-size: 12px;
  }
}
</style>
